# -*- coding: utf-8 -*-

import wx
from wx.lib import stattext
import  wx.lib.colourselect as  csel

# ---------- My Modules ---------- #
import optionVar


class SettingsFrame(wx.MiniFrame):
	def __init__(self, parent, style=wx.DEFAULT_FRAME_STYLE):
		self.parent = parent
		title = 'Settings'
		wx.MiniFrame.__init__(self, parent, -1, title, style=style)
		
		# ---------- Notebook ---------- #
		Nb = wx.Notebook(self, -1, style=wx.NB_TOP)
		
		# ---------- Panels ---------- #
		appearancePanel=wx.Panel(Nb)
		advancedPanel=wx.Panel(Nb)
		
		labelW=60
		
		(fileList_sizer,
		 self.fileList_FontNameCtrl,
		 self.fileList_FontSizeCtrl,
		 self.fileList_FontColourCtrl,
		 self.fileList_BGColourCtrl) = self.AppearanceCtrl(
			appearancePanel,
			'FileList:',
			self.parent.GetFileListFont(),
			self.parent.GetFileListTextColour(),
			self.parent.GetFileListColour(),
			self.AppearanceReturn,
			fixedWidthOnly=False
			)
		
		(output_sizer,
		 self.output_FontNameCtrl,
		 self.output_FontSizeCtrl,
		 self.output_FontColourCtrl,
		 self.output_BGColourCtrl) = self.AppearanceCtrl(
			appearancePanel,
			'Output:',
			self.parent.GetOutputFont(),
			self.parent.GetOutputTextColour(),
			self.parent.GetOutputColour(),
			self.AppearanceReturn
			)
		
		# ---------- Editor Settings ---------- #
		(editor_main_sizer,
		 self.editor_main_FontNameCtrl,
		 self.editor_main_FontSizeCtrl,
		 self.editor_main_FontColourCtrl,
		 self.editor_main_BGColourCtrl) = self.AppearanceCtrl(
			appearancePanel,
			'Main Font:',
			self.parent.GetTabpadFont(),
			(0,0,0),
			(0,0,0),
			self.AppearanceReturn)
		
		appearanceBorder = wx.BoxSizer(wx.VERTICAL)
		appearanceBorder.Add(fileList_sizer, 0, wx.LEFT | wx.RIGHT | wx.TOP | wx.EXPAND, 5)
		appearanceBorder.Add(output_sizer, 0, wx.LEFT | wx.RIGHT | wx.TOP | wx.EXPAND, 5)
		appearanceBorder.Add(editor_main_sizer, 0, wx.LEFT | wx.RIGHT | wx.TOP | wx.EXPAND, 5)
		appearancePanel.SetSizer(appearanceBorder)
		
		# ---------- Socket ---------- #
		self.socketST =  wx.StaticText(advancedPanel, -1,
									   "Socket:",
									   size=(labelW,-1),
									   style = wx.ALIGN_RIGHT)
		self.socketAddrTC = wx.TextCtrl(advancedPanel, -1,
										  str(self.parent.serverHost))
		self.socketPortTC = wx.TextCtrl(advancedPanel, -1,
										  str(self.parent.serverPort))
		self.socketB = wx.Button(advancedPanel,-1,
								   "Set")
		self.socketSizer = wx.BoxSizer(wx.HORIZONTAL)
		self.socketSizer.Add( self.socketST, 0)
		self.socketSizer.Add( self.socketAddrTC, 1)
		self.socketSizer.Add( self.socketPortTC, 1)
		self.socketSizer.Add( self.socketB, 0)
		
		advancedBorder = wx.BoxSizer(wx.VERTICAL)
		advancedBorder.Add(self.socketSizer, 0, wx.LEFT | wx.RIGHT | wx.TOP | wx.EXPAND, 5)
		advancedPanel.SetSizer(advancedBorder)
		
		Nb.AddPage(appearancePanel, "Appearance")
		Nb.AddPage(advancedPanel, "Advanced")
		self.Show(True)
		
	def OnSetServerAddrPort(self, event):
		self.parent.serverHost = self.socketAddrTC.GetTextValue()
		self.parent.serverPort = int(self.socketPortTC.GetValue())
		optionVar.saveOption('Socket_Address', self.parent.serverHost)
		optionVar.saveOption('Socket_Port', self.parent.serverPort)
		
	def AppearanceCtrl(self, parent, titleText, currentFont, FontColour, BGColour, eventFunction, fixedWidthOnly = True):
		editorST = wx.StaticText(parent, -1,
									  titleText,
									  size = (60,-1),
									  style = wx.ALIGN_RIGHT)
		editorFontNameCB = wx.ComboBox(parent, wx.NewId(),
										value = currentFont.GetFaceName(),
										choices = self.GetFontList(fixedWidthOnly),
										style = wx.CB_DROPDOWN|wx.CB_READONLY|wx.CB_SORT)
		editorTextSizeCB = wx.ComboBox(parent, wx.NewId(),
											value = str(currentFont.GetPointSize()),
											choices = ['8','9','10','11','12','13','14'],
											style = wx.CB_DROPDOWN|wx.CB_READONLY,
											size= (40,-1)
											)
		editorFontColour = csel.ColourSelect(parent, -1, '', FontColour)
		editorBGColour = csel.ColourSelect(parent, -1, '', BGColour)
		editorSizer = wx.BoxSizer(wx.HORIZONTAL)
		editorSizer.Add( editorST, 0)
		editorSizer.Add( editorFontNameCB, 1)
		editorSizer.Add( editorTextSizeCB, 0)
		editorSizer.Add( editorFontColour, 0)
		editorSizer.Add( editorBGColour, 0)
		self.Bind(csel.EVT_COLOURSELECT, eventFunction, id=editorFontColour.GetId())
		self.Bind(csel.EVT_COLOURSELECT, eventFunction, id=editorBGColour.GetId())
		self.Bind(wx.EVT_COMBOBOX, eventFunction, editorFontNameCB) 
		self.Bind(wx.EVT_COMBOBOX, eventFunction, editorTextSizeCB)
		return (editorSizer,
				editorFontNameCB,
				editorTextSizeCB,
				editorFontColour,
				editorBGColour)
	
	def AppearanceReturn(self, event):
		if (event.GetId()==self.fileList_FontNameCtrl.GetId()
			or event.GetId()==self.fileList_FontSizeCtrl.GetId()):
			newFont = wx.Font(
				int(self.fileList_FontSizeCtrl.GetValue()),
				wx.DEFAULT,
				wx.NORMAL,
				wx.NORMAL,
				faceName= self.fileList_FontNameCtrl.GetValue()
			)
			self.parent.SetFileListFont(newFont)
			optionVar.saveOption('FileList_Size', newFont.GetPointSize())
			optionVar.saveOption('FileList_Family', newFont.GetFamily())
			optionVar.saveOption('FileList_Style', newFont.GetStyle())
			optionVar.saveOption('FileList_Weight', newFont.GetWeight())
			optionVar.saveOption('FileList_Font', newFont.GetFaceName())
			
		elif (event.GetId()==self.fileList_FontColourCtrl.GetId()):
			optionVar.saveOption('FileList_FontColour', event.GetValue())
			self.parent.SetFileListTextColour(event.GetValue())
			
		elif (event.GetId()==self.fileList_BGColourCtrl.GetId()):
			optionVar.saveOption('FileList_BGColour', event.GetValue())
			self.parent.SetFileListColour(event.GetValue())
			
		elif (event.GetId()==self.output_FontNameCtrl.GetId()
			or event.GetId()==self.output_FontSizeCtrl.GetId()):
			newFont = wx.Font(
				int(self.output_FontSizeCtrl.GetValue()),
				wx.DEFAULT,
				wx.NORMAL,
				wx.NORMAL,
				faceName= self.output_FontNameCtrl.GetValue()
			)
			self.parent.SetOutputFont(newFont)
			optionVar.saveOption('Output_Size', newFont.GetPointSize())
			optionVar.saveOption('Output_Family', newFont.GetFamily())
			optionVar.saveOption('Output_Style', newFont.GetStyle())
			optionVar.saveOption('Output_Weight', newFont.GetWeight())
			optionVar.saveOption('Output_Font', newFont.GetFaceName())
			
		elif (event.GetId()==self.output_FontColourCtrl.GetId()):
			optionVar.saveOption('Output_FontColour', event.GetValue())
			self.parent.SetOutputTextColour(event.GetValue())
			
		elif (event.GetId()==self.output_BGColourCtrl.GetId()):
			optionVar.saveOption('Output_BGColour', event.GetValue())
			self.parent.SetOutputColour(event.GetValue())
			
		elif (event.GetId()==self.editor_main_FontNameCtrl.GetId()
		 or event.GetId()==self.editor_main_FontSizeCtrl.GetId()):
			newFont = wx.Font(
				int(self.editor_main_FontSizeCtrl.GetValue()),
				wx.DEFAULT,
				wx.NORMAL,
				wx.NORMAL,
				faceName= self.editor_main_FontNameCtrl.GetValue()
			)
			self.parent.SetTabpadFont(newFont)
			optionVar.saveOption('Editor_Size', newFont.GetPointSize())
			optionVar.saveOption('Editor_Family', newFont.GetFamily())
			optionVar.saveOption('Editor_Style', newFont.GetStyle())
			optionVar.saveOption('Editor_Weight', newFont.GetWeight())
			optionVar.saveOption('Editor_Font', newFont.GetFaceName())
			
		elif (event.GetId()==self.editor_main_FontColourCtrl.GetId()):
			optionVar.saveOption('Editor_FontColour', event.GetValue())
			self.parent.SetTabpadTextColour(event.GetValue())
			
		elif (event.GetId()==self.editor_main_BGColourCtrl.GetId()):
			optionVar.saveOption('Editor_BGColour', event.GetValue())
			self.parent.SetTabpadColour(event.GetValue())
			
	def GetFontList(self, fixedWidthOnly = True):
		fe = FE()
		fe.EnumerateFacenames(wx.FONTENCODING_SYSTEM ,  fixedWidthOnly = fixedWidthOnly)
		return fe.GetFontList()


class FE(wx.FontEnumerator):
	def __init__(self):
		wx.FontEnumerator.__init__(self)
		self.fontList=[]
		
	def OnFacename(self, fontname):
		self.fontList.append(fontname)
		
	def GetFontList(self):
		return self.fontList

# ---------- Not Codet yet ---------- #
# Set/Get Editor Main text colour
# Editor Main colour optionVar

